package com.jeesite.modules.app.util.time;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

public class DateUtils {
      public static LocalDateTime date2LocalDateTime(Date date){
    	  if(null == date){
    		  return null;
    	  }
    	  return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
      }
      public static Date localDateTime2Date(LocalDateTime localDateTime){
    	  if(null == localDateTime){
    		  return null;
    	  }
    	  return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
      }
      /**
       * 相差的天数
       * @param d1
       * @param d2
       * @return
       */
      public static Long betweenDays(LocalDateTime d1,LocalDateTime d2){
    	  Duration duration=Duration.between(d1, d2);
    	  return duration.toDays();
      }
      /**
       * 相差的小时数
       * @param d1
       * @param d2
       * @return
       */
      public static Long betweenHours(LocalDateTime d1,LocalDateTime d2){
    	  Duration duration=Duration.between(d1, d2);
    	  return duration.toHours();
      }
      /**
       * 相差的分钟数
       * @param d1
       * @param d2
       * @return
       */
      public static Long betweenMinutes(LocalDateTime d1,LocalDateTime d2){
    	  Duration duration=Duration.between(d1, d2);
    	  return duration.toMinutes();
      }
      /**
       * 相差的毫秒数
       * @param d1
       * @param d2
       * @return
       */
      public static Long betweenMillis(LocalDateTime d1,LocalDateTime d2){
    	  Duration duration=Duration.between(d1, d2);
    	  return duration.toMillis();
      }
      /**
       * 相差的纳秒数
       * @param d1
       * @param d2
       * @return
       */
      public static Long betweenNanos(LocalDateTime d1,LocalDateTime d2){
    	  Duration duration=Duration.between(d1, d2);
    	  return duration.toNanos();
      }
      public static void main(String[] args) {
    	  LocalDateTime end = LocalDateTime.of(2020, 4, 6, 19, 59,59);
    	  LocalDateTime localDate2 = LocalDateTime.now();
    	  Long betweenDays = betweenDays(localDate2,end);
    	  Long betweenHours=betweenHours(localDate2, end);
    	  Long betweenMinutes=betweenMinutes(localDate2, end);
    	  Long betweenMillis=betweenMillis(localDate2,end);
    	  Long s=betweenMillis/1000;
    	  Long ss=0l;
    	  Long mm=0l;
    	  Long hh=0l;
    	  if(s>60) {
    		  ss=s%60;
    		  Long m=s/60;
    		  if(m>60) {
        		  mm=m%60;
        		  hh=m/60;
        	  }else {
        		  mm=m;
        	  }
    	  }else {
    		  ss=s;
    	  }
    	  System.out.println(hh+":"+mm+":"+ss);
//    	  System.out.println(betweenDays+":"+betweenHours+":"+betweenMinutes+":"+betweenMillis);
	}
}
